﻿using System;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Collections;
using System.Text.RegularExpressions;
using System.Threading;


public abstract class AccessHelper
{
    private static readonly string path = AppDomain.CurrentDomain.BaseDirectory + @"\App_Data\chat.mdb";
    public static readonly string ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + path;

    private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());

    public static int ExecuteNonQuery(CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
    {
        return ExecuteNonQuery(ConnectionString, cmdType, cmdText, commandParameters);
    }

    public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
    {
        OleDbCommand cmd = new OleDbCommand();
        using (OleDbConnection conn = new OleDbConnection(connectionString))
        {
            PrepareCommand(cmd, conn, cmdType, cmdText, commandParameters);
            int val = cmd.ExecuteNonQuery();
            cmd.Parameters.Clear();
            return val;
        }
    }

    public static int ExecuteNonQuery(OleDbConnection connection, CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
    {
        OleDbCommand cmd = new OleDbCommand();
        PrepareCommand(cmd, connection, cmdType, cmdText, commandParameters);
        int val = cmd.ExecuteNonQuery();
        cmd.Parameters.Clear();
        return val;
    }


    public static OleDbDataReader ExecuteReader(CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
    {
        return ExecuteReader(ConnectionString, cmdType, cmdText, commandParameters);
    }

    
    public static OleDbDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
    {
        OleDbCommand cmd = new OleDbCommand();
        OleDbConnection conn = new OleDbConnection(connectionString);
        try
        {
            PrepareCommand(cmd, conn, cmdType, cmdText, commandParameters);
            OleDbDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            cmd.Parameters.Clear();
            return rdr;
        }
        catch
        {
            conn.Close();
            throw;
        }
    }
    
     
    public static OleDbDataReader ExecuteReader(OleDbConnection connection, CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
    {
        OleDbCommand cmd = new OleDbCommand();
        PrepareCommand(cmd, connection, cmdType, cmdText, commandParameters);
        OleDbDataReader rdr = cmd.ExecuteReader();
        cmd.Parameters.Clear();
        return rdr;
    }

    public static object ExecuteScalar(CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
    {
        return ExecuteScalar(ConnectionString, cmdType, cmdText, commandParameters);
    }

    public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
    {
        OleDbCommand cmd = new OleDbCommand();

        using (OleDbConnection connection = new OleDbConnection(connectionString))
        {
            PrepareCommand(cmd, connection, cmdType, cmdText, commandParameters);
            object val = cmd.ExecuteScalar();
            cmd.Parameters.Clear();
            return val;
        }
    }
    public static object ExecuteScalar(OleDbConnection connection, CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
    {

        OleDbCommand cmd = new OleDbCommand();

        PrepareCommand(cmd, connection, cmdType, cmdText, commandParameters);
        object val = cmd.ExecuteScalar();
        cmd.Parameters.Clear();
        return val;
    }
    /*
    public static OleDbDataAdapter ExecuteAdapter(CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
    {
        return ExecuteAdapter(ConnectionString, cmdType, cmdText, commandParameters);
    }
    public static OleDbDataAdapter ExecuteAdapter(string connectionString, CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
    {
        OleDbCommand cmd = new OleDbCommand();
        using (OleDbConnection connection = new OleDbConnection(connectionString))
        {
            PrepareCommand(cmd, connection, cmdType, cmdText, commandParameters);

            OleDbDataAdapter ap = new OleDbDataAdapter();
            ap.SelectCommand = cmd;
            
        }
        return ap;

    }*/
    public static DataTable ExecuteTable(CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
    {
        return ExecuteTable(ConnectionString, cmdType, cmdText, commandParameters);
    }
    public static DataTable ExecuteTable(string connectionString, CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
    {
        OleDbCommand cmd = new OleDbCommand();

        using (OleDbConnection connection = new OleDbConnection(connectionString))
        {
            PrepareCommand(cmd, connection, cmdType, cmdText, commandParameters);

            OleDbDataAdapter ap = new OleDbDataAdapter();
            ap.SelectCommand = cmd;
            DataSet st = new DataSet();
            ap.Fill(st, "Result");
            cmd.Parameters.Clear();
            return st.Tables["Result"];
        }
    }
    public static DataSet ExecuteDataSet(CommandType cmdType, string cmdText,int pageIndex,int pageSize, params OleDbParameter[] commandParameters)
    {
        return ExecuteDataSet(ConnectionString, cmdType, cmdText,pageIndex,pageSize, commandParameters);
    }
    public static DataSet ExecuteDataSet(string connectionString, CommandType cmdType, string cmdText, int pageIndex, int pageSize, params OleDbParameter[] commandParameters)
    {
        OleDbCommand cmd = new OleDbCommand();
         
        using (OleDbConnection connection = new OleDbConnection(connectionString))
        {
            PrepareCommand(cmd, connection, cmdType, cmdText, commandParameters);

            OleDbDataAdapter ap = new OleDbDataAdapter();
            ap.SelectCommand = cmd;
            DataSet st = new DataSet();
            ap.Fill(st, (pageIndex - 1) * pageSize, pageSize, "DataSetPage");
            cmd.Parameters.Clear();
            return st;
        }
    }
    /*
    public static DataTable ExecuteTable(OleDbConnection connection, CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
    {

        OleDbCommand cmd = new OleDbCommand();

        PrepareCommand(cmd, connection, cmdType, cmdText, commandParameters);
        OleDbDataAdapter ap = new OleDbDataAdapter();
        ap.SelectCommand = cmd;
        DataSet st = new DataSet();
        ap.Fill(st, "Result");
        cmd.Parameters.Clear();
        return st.Tables["Result"];
    }
    */
    public static void CacheParameters(string cacheKey, params OleDbParameter[] commandParameters)
    {
        parmCache[cacheKey] = commandParameters;
    }


    public static OleDbParameter[] GetCachedParameters(string cacheKey)
    {
        OleDbParameter[] cachedParms = (OleDbParameter[])parmCache[cacheKey];

        if (cachedParms == null)
            return null;

        OleDbParameter[] clonedParms = new OleDbParameter[cachedParms.Length];

        for (int i = 0, j = cachedParms.Length; i < j; i++)
            clonedParms[i] = (OleDbParameter)((ICloneable)cachedParms[i]).Clone();

        return clonedParms;
    }

    private static void PrepareCommand(OleDbCommand cmd, OleDbConnection conn, CommandType cmdType, string cmdText, OleDbParameter[] cmdParms)
    {
        if (conn.State != ConnectionState.Open)
            conn.Open();

        cmd.Connection = conn;
        cmd.CommandText = cmdText;

        cmd.CommandType = cmdType;

        if (cmdParms != null)
        {
            foreach (OleDbParameter parm in cmdParms)
                cmd.Parameters.Add(parm);
        }
    }
}

